package com.bonc.rms.service.impl.tms;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bonc.odk.logmanager.LogManager;
import com.bonc.rms.mapper.EmployeeRRServiceMapper;
import com.bonc.rms.service.tms.EmployeeRRService;
import com.bonc.rms.utils.impl.EmployeeRRUtilImpl;
import com.bonc.rms.utils.impl.WastageInfoUtilImpl;

@Service
public class EmployeeRRServiceimpl implements EmployeeRRService {
	@Autowired
	private EmployeeRRServiceMapper employeeRRMapper;

	@Autowired
	EmployeeRRUtilImpl employeeRRUtilImpl;
	@Autowired
	WastageInfoUtilImpl wastageInfoUtilImpl;

	/**
	 * 员工增长率
	 */
	@Override
	public List<String> getEmployeeGrowth(Integer departmentStandardId,
			String startTime, String endTime) {
		LogManager.Method("In  Service : EmployeeRRServiceimpl -> getEmployeeGrowth");
		DecimalFormat decimalFormat = new DecimalFormat(".00");

		List<String> andRate = new ArrayList<>();

		List<Integer> departmentIdlist = new ArrayList<>();
		departmentIdlist = wastageInfoUtilImpl
				.getDepartmentId(departmentStandardId);
		if (departmentIdlist == null || departmentIdlist.size() == 0) {
			if (departmentStandardId > 2) {
				if ((startTime == null || startTime.equals(""))
						& (endTime == null || endTime.equals(""))) {
					for (int i = 1; i < 13; i++) {
						andRate.add("0");
					}
				} else {
					List<String> timeSpan = new ArrayList<>();
					timeSpan = getTimeSpan(startTime, endTime);
					for (String dataStr : timeSpan) {
						andRate.add("0");
					}
				}
			} else {

				if ((startTime == null || startTime.equals(""))
						& (endTime == null || endTime.equals(""))) {

					// 默认无时间跨度无部门号

					Date date = new Date();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

					String year = sdf.format(date).substring(0, 4);

					int lastYear = Integer.parseInt(year) - 1;

					for (int i = 1; i < 13; i++) {
						int addE = 0;
						int lastYearE = 0;
						float b = (float) 0.0;
						// // 本期增加员工数

						addE = employeeRRMapper.getAddEmployee(year, i + "",
								departmentIdlist, departmentIdlist.size());

						lastYearE = employeeRRMapper.getLastYearEmployee(
								lastYear, i + "", departmentIdlist,
								departmentIdlist.size());

						if (lastYearE != 0) {

							b = addE * 100 / lastYearE;
						}

						String rate = decimalFormat.format(b);

						andRate.add(rate);

					}

				} else {

					// 无部门号有时间跨度

					List<String> timeSpan = new ArrayList<>();
					timeSpan = getTimeSpan(startTime, endTime);
					for (String dataStr : timeSpan) {

						String yearStr = dataStr.substring(0, 4);
						String monthStr = dataStr.substring(
								dataStr.indexOf("年") + 1, dataStr.indexOf("月"));
						int lastYear = Integer.parseInt(yearStr) - 1;

						int addE = 0;
						int lastYearE = 0;
						float b = (float) 0.0;

						addE = employeeRRMapper.getAddEmployee(yearStr,
								monthStr, departmentIdlist,
								departmentIdlist.size());

						lastYearE = employeeRRMapper.getLastYearEmployee(
								lastYear, monthStr, departmentIdlist,
								departmentIdlist.size());
						if (lastYearE != 0) {

							b = addE * 100 / lastYearE;
						}
						String rate = decimalFormat.format(b);

						andRate.add(rate);
					}
				}
				;

			}

		} else {

			if ((startTime == null || startTime.equals(""))
					& (endTime == null || endTime.equals(""))) {

				// 有部门号无时间跨度

				Date date = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

				String year = sdf.format(date).substring(0, 4);

				int lastYear = Integer.parseInt(year) - 1;

				for (int i = 0; i < 13; i++) {

					float yearEAndSum = 0;
					float lastYearESum = 0;
					float b = (float) 0.0;

					yearEAndSum = employeeRRMapper.getAddEmployee(year, i + "",
							departmentIdlist, departmentIdlist.size());

					lastYearESum = employeeRRMapper.getLastYearEmployee(
							lastYear, i + "", departmentIdlist,
							departmentIdlist.size());

					if (lastYearESum != 0) {

						b = yearEAndSum * 100 / lastYearESum;
					}

					String rate = decimalFormat.format(b);

					andRate.add(rate);

				}

			} else {
				// 有部门号有时间跨度
			
				List<String> timeSpan = new ArrayList<>();
				timeSpan = getTimeSpan(startTime, endTime);
				for (String dataStr : timeSpan) {
					String yearStr = dataStr.substring(0, 4);
					String monthStr = dataStr.substring(
							dataStr.indexOf("年") + 1, dataStr.indexOf("月"));
					int lastYear = Integer.parseInt(yearStr) - 1;
					float yearEAnd = 0;
					float lastYearE = 0;
					float b = (float) 0.0;

					yearEAnd = employeeRRMapper
							.getAddEmployee(yearStr, monthStr,
									departmentIdlist, departmentIdlist.size());

					lastYearE = employeeRRMapper
							.getLastYearEmployee(lastYear, monthStr,
									departmentIdlist, departmentIdlist.size());

					if (lastYear != 0) {

						b = yearEAnd * 100 / lastYearE;
					}
					String rate = decimalFormat.format(b);
					andRate.add(rate);

				}
			}

		}
		LogManager.Method("Out  Service : EmployeeRRServiceimpl -> getEmployeeGrowth");
		return andRate;
		
	}

	/**
	 * 获取跨区时间年月份
	 * 
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public List<String> getTimeSpan(String startTime, String endTime) {
		LogManager.Method("In  Service : EmployeeRRServiceimpl -> getTimeSpan");
	
		String startYear = startTime.substring(0, 4);

		String start = (startTime.substring(5, 6)).equals("0") ? startTime
				.substring(6, 7) : startTime.substring(5, 7);

		int startT = Integer.parseInt(start);
		int startYearT = Integer.parseInt(startYear);

		String end = (endTime.substring(5, 6)).equals("0") ? endTime.substring(
				6, 7) : endTime.substring(5, 7);
		String endYear = endTime.substring(0, 4);
		int endT = Integer.parseInt(end);
		int endYearT = Integer.parseInt(endYear);

		List<String> datesList = new ArrayList<String>();

		if (startYearT == endYearT) {
			for (int i = startT; i <= endT; i++) {

				datesList.add(startYearT + "年" + i + "月");

			}
		} else {
			for (int i = startT; i <= 12; i++) {

				datesList.add(startYearT + "年" + i + "月");
			}
			int y = endYearT - startYearT;

			for (int i = 1; i < y; i++) {

				int year = startYearT + i;
				for (int j = 1; j < 13; j++) {

					datesList.add(year + "年" + j + "月");
				}

			}

			for (int i = 1; i <= endT; i++) {

				datesList.add(endYearT + "年" + i + "月");
			}
		}
		LogManager.Method("Out  Service : EmployeeRRServiceimpl -> getTimeSpan");
		return datesList;
	}

	/**
	 * 总工资增长率
	 */
	@Override
	public List<String> getSalaryGrowt(Integer departmentId, String startTime,
			String endTime) {
		LogManager.Method("In  Service : EmployeeRRServiceimpl -> getSalaryGrowt");
		List<String> salaryList = new ArrayList<>();
		// 默认无时间跨度无部门号
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		String yearStr = sdf.format(date).substring(0, 4);
		int yearInt = Integer.parseInt(yearStr);

		int lastYearInt = yearInt - 1;
		String lastYear = Integer.toString(lastYearInt);

		int beforeLastYearInt = lastYearInt - 1;
		String beforeLastYear = Integer.toString(beforeLastYearInt);

		if ((startTime == null || startTime.equals(""))
				& (endTime == null || endTime.equals(""))) {
			// 获取每个年份的在职员工
			List<String> beforeLastYearElist = new ArrayList<>();
			beforeLastYearElist = employeeRRUtilImpl.getActiveDutyYear(
					beforeLastYear, departmentId);
			List<String> lastYearEList = new ArrayList<>();
			lastYearEList = employeeRRUtilImpl.getActiveDutyYear(lastYear,
					departmentId);
			if (beforeLastYearElist == null || beforeLastYearElist.size() == 0) {
				salaryList.add("0");
			} else {
				// 获取计算这些员工总工资的必要信息
				// 入职时间 试用期转正时间 离职时间 调薪日期(list)
//				Map<String, Map<String, Object>> beforeLastYearEMap = employeeRRUtilImpl
//						.getEmployeeInfo(beforeLastYearElist, beforeLastYear);
				// 计算总工资
				String beforeLastYearTotalWages = employeeRRUtilImpl
						.getTotalWages(beforeLastYearElist, beforeLastYear);
				salaryList.add(beforeLastYearTotalWages);
			}
			if (lastYearEList == null || lastYearEList.size() == 0) {
				salaryList.add("0");
			} else {
//				Map<String, Map<String, Object>> lastYearEListMap = employeeRRUtilImpl
//						.getEmployeeInfo(lastYearEList, lastYear);
				String lastYearTotalWages = employeeRRUtilImpl.getTotalWages(
						lastYearEList, lastYear);
				salaryList.add(lastYearTotalWages);

			}
			salaryList.add("0");

		} else {
			int startTimeInt = Integer.parseInt(startTime);
			int endTimeInt = Integer.parseInt(endTime);
			for (int i = startTimeInt; i <= endTimeInt; i++) {
				if (i > yearInt) {
					salaryList.add("0");
				} else {

					List<String> timeList = new ArrayList<>();
					timeList = employeeRRUtilImpl.getActiveDutyYear(i + "",
							departmentId);
					if (timeList == null || timeList.size() == 0) {
						salaryList.add("0");
					} else {
//						Map<String, Map<String, Object>> timeMap = employeeRRUtilImpl
//								.getEmployeeInfo(timeList, i + "");
						String timeTotalWages = employeeRRUtilImpl
								.getTotalWages(timeList, i + "");
						salaryList.add(timeTotalWages);

					}
				}

			}
		}
		LogManager.Method("Out  Service : EmployeeRRServiceimpl -> getSalaryGrowt");
		return salaryList;
	}
}
